<template>
  <div>
    <div>
      <el-input v-model="jl.name" placeholder="添加职称等级.." suffix-icon="el-icon-plus" style="width: 300px;" size="small"/>
      <el-select v-model="jl.titleLevel" placeholder="请选择职称等级" size="small" style="margin-left: 8px; margin-right: 8px">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addJoblevel">添加职称</el-button>
    </div>
    <div class="JoblevelManaMain">
      <el-table
          :data="jls"
          stripe
          border
          size="small"
          @selection-change="handleSelectionChange"
          style="width: 70%">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            label="职称等级"
            width="80">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用"
            width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间"
            width="150">
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button size="small" @click="showEditView(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-button type="danger" size="small" style="margin-top: 8px"
               :disabled="multipleSelection.length == 0"
               @click="deleteMany">批量删除</el-button>
    <el-dialog
        title="编辑职称"
        :visible.sync="dialogVisible"
        width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职称名称</el-tag>
          </td>
          <td>
            <el-input size="small" v-model="updateJoblevel.name" />
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职称等级</el-tag>
          </td>
          <td>
            <el-select v-model="updateJoblevel.titleLevel" placeholder="请选择职称等级" size="small">
              <el-option
                  v-for="item in titleLevels"
                  :key="item"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch
                v-model="updateJoblevel.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="启用"
                inactive-text="不启用">
            </el-switch>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "JoblevelMana",
  data(){
    return{
      // 这个用于添加
      jl:{
        name:'',
        titleLevel:''
      },
      // 这个用于存放固定的职称等级
      titleLevels:[
          '正高级',
          '副高级',
          '中级',
          '初级',
          '员级'
      ],
      updateJoblevel:{
        id:'',
        name:'',
        titleLevel: '',
        enabled:''
      },
      // 这个用于存放后端查询结果
      jls:[],
      // 这个用于控制dialog弹窗是否弹出
      dialogVisible: false,
      // 这个用于储存多选框选中的行
      multipleSelection: [],
    }
  },
  methods:{
    initJobleavel(){
      this.getRequest('/system/basic/joblevel/').then(resp =>{
        if (resp){
          this.jls = resp
        }
      })
    },
    addJoblevel(){
      if (this.jl.name && this.jl.titleLevel){
        this.postRequest('/system/basic/joblevel/',this.jl).then(resp =>{
          if (resp){
            this.jl.name = '';
            this.jl.titleLevel = '';
            this.initJobleavel()
          }
        })
      }
    },
    doUpdate(){
      if (this.updateJoblevel.titleLevel && this.updateJoblevel.name){
        this.putRequest('/system/basic/joblevel/',this.updateJoblevel).then(resp =>{
          if (resp){
            this.initJobleavel();
            this.dialogVisible = false;
          }
        })
      } else {
        this.$message.warning("请正确输入信息");
      }
    },
    showEditView(index,data){
      this.dialogVisible = true;
      this.updateJoblevel.id = data.id;
      this.updateJoblevel.name = data.name;
      this.updateJoblevel.titleLevel = data.titleLevel;
      this.updateJoblevel.enabled = data.enabled;
    },
    handleDelete(index,data){
      this.$confirm('此操作将永久删除['+data.name+']职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/joblevel/'+data.id).then(resp =>{
          if (resp){
            this.initJobleavel();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 这个函数会接收到被选中的行的数据
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    deleteMany(){
      this.$confirm('此操作将永久删除['+this.multipleSelection.length+']个职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = "?";
        this.multipleSelection.forEach(item =>{
          ids+='ids='+item.id+'&';
        })
        this.deleteRequest('/system/basic/joblevel/'+ids).then(resp =>{
          if (resp){
            this.initJobleavel();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  created() {
    this.initJobleavel();
  }
}
</script>

<style scoped>

.JoblevelManaMain{
  margin-top: 10px;
}

.updateJoblevelInput{
  width: 200px;
  margin-left: 8px;
}

.dialogItem{
  margin: 5px;
}

.updateJoblevelSelect{
  width: 200px;
  margin-left: 8px;
}

</style>